package com.wds.common.utils;

import com.wds.domain.entity.LoginUser;
import com.wds.domain.entity.User;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author WDs , wds8.24@outlook.com
 * @version 1.0
 * @since 2022-11-01 11:33
 * security 的工具类
 */
public class SecurityUtils {

    private SecurityUtils(){}

    public static LoginUser getCurrentLoginUser(){
        try {
            return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            return null;
        }
    }

    public static Long getCurrentUserId(){
        try {
            return ((LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getId();
        } catch (Exception e) {
            return -1L;
        }
    }
    public static User getCurrentUser(){
        try {
            return ((LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
        } catch (Exception e) {
            return null;
        }
    }
}
